<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\maintenance;

use app\adminapi\services\maintenance\LangTranslationService;
use app\adminapi\validate\maintenance\LangTranslationValidate;
use app\common\services\TranslationService;
use Exception;
use kitego\base\BaseController;
use think\App;
use think\response\Json;

class LangTranslationController extends BaseController
{
    public array $safeAction = ['listTranslationJson'];

    private LangTranslationService $service;

    public function __construct(App $app, LangTranslationService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 翻译列表
     * @throws Exception
     */
    public function list(): Json
    {
        $params = $this->params;
        $page = !empty($params['page']) ? $params['page'] : 1;
        $pageSize = !empty($params['pageSize']) ? $params['pageSize'] : 10;
        $keywords = !empty($params['keywords']) ? $params['keywords'] : null;
        $langId = !empty($params['langId']) ? $params['langId'] : null;

        return $this->service->list($page, $pageSize, $keywords, $langId);
    }

    /**
     * 编辑翻译
     * @throws Exception
     */
    public function edit(): Json
    {
        $params = $this->params;
        $params['id'] = $params['id'] ?? null;
        Validate(LangTranslationValidate::class)->scene('edit')->check($params);

        return $this->service->edit(
            $params['id'], $params['scene'], $params['sort'], $params['originalText'], $params['translationList'], $params['isDelete']
        );
    }

    /**
     * 批量删除
     */
    public function batchDel(): Json
    {
        $params = $this->params;
        if (!isset($params['ids']) || !is_array($params['ids'])) {
            exception('参数类型必须是数组');
        }

        return $this->service->batchDel($params['ids']);
    }

    /**
     * 机器翻译
     */
    public function robotTranslate(): Json
    {
        $params = $this->params;
        if (empty($params['originalText'])) exception('参数错误');
        $params['langId'] = !empty($params['langId']) ? $params['langId'] : null;

        return $this->service->robotTranslate($params['originalText'], $params['langId']);
    }

    /**
     * 语言包
     */
    public function listTranslationJson(): Json
    {
        return app()->make(TranslationService::class)->listTranslationJson();
    }
}